<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * Adminlog Controller
 * Build by Phuc Nguyen
 * Contact : nguyenvanphuc0626@gmail.com
 */

class Sales_model extends MY_Model{
	
	public function __construct(){
		parent::__construct();
		$this->table_name = 'book_sales';
	}
        
        public function apiUpdateDb($params){
                $sql    = "INSERT IGNORE INTO `book_sales_phone` (`phone`) VALUES ".$params;
                //die($sql);
                $this->db->query($sql);
                return $this->db->affected_rows();
        }
        
        public function updateApiPhone($params){
                $sql    = "REPLACE INTO `book_sales_phone` (`phone`,`status`) VALUES ".$params;
                //die($sql);
                $this->db->query($sql);
                return $this->db->affected_rows();
        }
        
        public function deleteApiPhone($params){
           foreach($params as $values){
                $sql    = "DELETE FROM book_sales_phone WHERE phone = '".$values."'";
                $this->db->query($sql);
           }
        }
        
        public function apiUpdateDbCallBusy(){
                $sql = "UPDATE book_sales_phone SET status = 0 WHERE waiting = 0 AND status = 2";
                //die($sql);
                $this->db->query($sql);
                return $this->db->affected_rows();
        }
        
//        public function apiDeleteDbCallBusy(){
//                $sql = "DELETE FROM `book_sales` WHERE `phone_callstatus` = 1 OR `phone_callstatus` = 3";
//                $this->db->query($sql);
//        }
        
        public function updateStatusPhone($params1,$params2){
                $date = date('Y-m-d H:i:s',time());
                $sql = 'UPDATE `book_sales_phone` SET `status` = 2 , `waiting` = "'.$params2.'", lastupdated = "'.$date.'" WHERE `phone` = "'.$params1.'"';
                //die($sql);
                $this->db->query($sql);
                return $this->db->affected_rows();
        }
        
        public function search($params){
            $sql = "SELECT tbl.name,tbl.phone,tbl.note_customers,tbl.note_staffs,tbl.frequency,tbl.lastupdated, type.name AS type, province.name AS province, status.name AS phone_callstatus, area.area_name AS area, communication.communication_name AS communication, callquality.callquality_name AS callquality, interested.interested_name AS interested
                FROM {$this->table_name} AS tbl
                LEFT JOIN book_type_customers AS type ON type.id = tbl.type
                LEFT JOIN book_province AS province ON province.id = tbl.province
                LEFT JOIN book_callstatus AS status ON status.id = tbl.phone_callstatus
                LEFT JOIN book_area AS area ON area.id = tbl.area
                LEFT JOIN book_communication AS communication ON communication.id = tbl.communication
                LEFT JOIN book_callquality AS callquality ON callquality.id = tbl.callquality
                LEFT JOIN book_interested AS interested ON interested.id = tbl.interested
                
                WHERE tbl.phone LIKE '{$params['phone']}'
                ";
                if($this->session->userdata('group_id')!=1){
                    $sql .= " AND tbl.owner = {$params['owner']}";
                }
                $sql .= ' ORDER BY tbl.lastupdated DESC';
            $query  = $this->db->query($sql);
//            echo $this->db->last_query();exit;
            return $query->result();
        }
        
        public function statistic($params){
                $this->db->select('count(tbl.owner) as total, tbl.phone_callstatus, tbl.owner,tbl.lastupdated');
		$this->db->from($this->table_name . ' AS tbl');
		
		$this->db->where('tbl.owner', $params->user_id);
                $this->db->where('tbl.lastupdated > ', $params->begin);
                $this->db->where('tbl.lastupdated < ', $params->end);
		$this->db->group_by('tbl.phone_callstatus');
                $this->db->having('tbl.phone_callstatus > 0');
                
		$query = $this->db->get();
		return $query->result();
        }

        public function init_data($right){
		return array(
                                array(
						'name' => 'id',
						'label' => $this->lang->line('number'),
						'width' => '5%',
						'sort'  => 'DESC',
						'searchoptions' => array('type' => 'int')
				),
                                array(
						'name' => 'phone',
						'label' => $this->lang->line('phone'),
						'width' => '5%',
						'sort'  => false,
						'searchoptions' => false
				),array(
						'name' => 'name',
						'label' => $this->lang->line('name'),
						'width' => '5%',
						'sort'  => FALSE,
						'searchoptions' => false
				),array(
						'name' => 'note_customers',
						'label' => $this->lang->line('note_customers'),
						'width' => '10%',
						'sort'  => FALSE,
						'searchoptions' => false
				),array(
						'name' => 'note_staffs',
						'label' => $this->lang->line('note_staffs'),
						'width' => '10%',
						'sort'  => FALSE,
						'searchoptions' => false
				),array(
						'name' 	=> 'lastupdated',
						'label' => $this->lang->line('lastupdated'),
						'width' => '10%',
						'sort'  => false,
						'searchoptions' => false
				),array(
						'name'  => 'button',
						'width' => '10%',
						'sort'  => FALSE,
						'label'  => FALSE,
						'searchoptions' => false
				)
		);
	}
	
	public function json_data($controller, $right){
		$this->datatables
		->select("id,customerID,name,note_customers,phone,note_staffs,owner,lastupdated")
		->from($this->table_name);
                        
                if($this->session->userdata['group_id'] != 1){
                    $this->datatables->where('owner',$this->session->userdata['user_id'])->where('phone_callstatus','3');
                }
	
		$this->datatables->set_produce_output(false);
		$ouput = $datatables = $this->datatables->generate();
		unset($ouput['aaData']);
		$ouput['aaData'] = array();             

		foreach($datatables['aaData'] as $item){
			$ouput['aaData'][] = array(
                                        $item['id'],
					$item['phone'],
                                        $item['name'],
                                        $item['note_customers'],
                                        $item['note_staffs'],
                                        date("d-m-Y H:i:s",$item['lastupdated']),
                                        $this->add_button_sales($controller, $right, $item)
			);
		}
	
		return json_encode($ouput);
	}
}